package com.example.saveword.dao;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.example.saveword.model.User;
import com.example.saveword.utils.JsonUtils;
import com.example.saveword.utils.SPUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class UserDao {
    private DBOpenHelper helper;
    private SQLiteDatabase db;
    private Context context;
    //构造方法
    public UserDao(Context context){
        this.context=context;
    }
    public void insert(User user)
    {
        helper=new DBOpenHelper(context);
        db=helper.getReadableDatabase();

        ContentValues values=new ContentValues();
        values.put("username",user.getUsername());
        values.put("password",user.getPassword());
        values.put("phone",user.getPhone());
        db.insert("user", null, values);
        db.close();
        helper.close();
    }
    //更新方法
    public void update(User user)  {
        helper=new DBOpenHelper(context);
        db=helper.getReadableDatabase();

        ContentValues values=new ContentValues();
        values.put("_id",user.get_id());
        values.put("username",user.getUsername());
        values.put("password",user.getPassword());
        values.put("phone",user.getPhone());
        values.put("wordType",user.getWordType());
        db.update("user",values,"_id=1",null);
        //关闭数据库
        db.close();
        helper.close();
    }
    //获取id
    public Integer getId(String user)
    {
        helper=new DBOpenHelper(context);
        db=helper.getReadableDatabase();
        String sql=String.format("select * from user where _id= '%s'", user);
        Cursor cursor=db.rawQuery(sql,null);
        System.out.println(cursor.getCount());
        if (cursor.moveToNext()){
            @SuppressLint("Range") Integer id=cursor.getInt(cursor.getColumnIndex("_id"));
            //关闭游标和数据库
            cursor.close();
            db.close();
            helper.close();
            return id;
        }
        //关闭游标和数据库
        cursor.close();
        db.close();
        helper.close();
        return null;
    }
    //获取词书
    public String getWordType(String user) {
        helper=new DBOpenHelper(context);
        db=helper.getReadableDatabase();
        String sql="select * from user where username= ?";

        Cursor cursor=db.rawQuery(sql,new String[]{user});
        System.out.println(cursor.getCount());
        if (cursor.moveToNext()){
            @SuppressLint("Range") String wordType=cursor.getString(cursor.getColumnIndex("wordType"));
            //关闭游标和数据库
            cursor.close();
            db.close();
            helper.close();
            return wordType;
        }
        //关闭游标和数据库
        cursor.close();
        db.close();
        helper.close();
        return null;
    }

    //换词书
    public void updateWordType(String user, String wordType) {
        helper=new DBOpenHelper(context);
        db=helper.getReadableDatabase();
        ContentValues values=new ContentValues();
        values.put("wordType",wordType);
        int cnt = db.update("user",values, String.format((String) "username= '%s'", user),null);
        //关闭数据库
        Log.d("test", String.valueOf(cnt));
        db.close();
        helper.close();
    }

    // 登录
    public HashMap login(String username, String password) {
        helper = new DBOpenHelper(context);
        db = helper.getReadableDatabase();
        // 找到对应的号码
        String sql = "select * from user where username = ?";
        Cursor cursor = db.rawQuery(sql, new String[]{username});
        HashMap res = new HashMap();
        if(cursor.moveToNext()) {
            @SuppressLint("Range") int id = cursor.getInt(cursor.getColumnIndex("_id"));
            @SuppressLint("Range") String name = cursor.getString(cursor.getColumnIndex("username"));
            @SuppressLint("Range") String pwd = cursor.getString(cursor.getColumnIndex("password"));
            @SuppressLint("Range") String phone = cursor.getString(cursor.getColumnIndex("phone"));
            @SuppressLint("Range") String wordType = cursor.getString(cursor.getColumnIndex("wordType"));
            if(password.equals(pwd)) {
                res.put("code", 200);
                res.put("id", id);
                res.put("username", name);
                res.put("password", pwd);
                res.put("phone", phone);
                res.put("wordType", wordType);
            } else {
                res.put("code", 500);
            }
        } else {
            res.put("code", 501);
        }
        // 关闭游标和数据库
        cursor.close();
        db.close();
        helper.close();
        return res;
    }

    // 查询某用户名是否存在
    public boolean usernameIsExist(String username) {
        helper = new DBOpenHelper(context);
        db = helper.getReadableDatabase();
        String sql = "select * from user where username = ?";
        Cursor cursor = db.rawQuery(sql, new String[]{username});
        boolean flag = false;
        if(cursor.moveToNext()) {
            @SuppressLint("Range") String name = cursor.getString(cursor.getColumnIndex("username"));
            if(name.equals(username)) {
                flag = true;
            }
        }
        // 关闭游标和数据库
        cursor.close();
        db.close();
        helper.close();
        return flag;
    }

    // 注册
    public int register(String username, String qqNumber, String passwd) {
        if(usernameIsExist(username)) {
            return 500; // 用户名已存在
        } else {
            User user = new User();
            user.setUsername(username);
            user.setPassword(passwd);
            user.setPhone(qqNumber);
            user.setWordType("CET4"); // 默认四级
            insert(user);
            return 200; // 注册成功
        }
    }
}
